package com.laicunba.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.laicunba.constant.FinancingTargetConstant;
import com.laicunba.pojo.FinancingTargetDO;
import com.laicunba.pojo.vo.PositionConditionVO;
import com.laicunba.service.FinancingTargetService;
import com.laicunba.util.StringUtil;

@Service
public class FinancingTargetServiceImpl extends BaseServiceImpl implements FinancingTargetService {

	@Override
	public Map<String, Integer> listStatusCount() {
		List<Map<String, Object>> list = queryDao.selectList("FINANCING_TARGET.listStatusCount", null);

		Map<String, Integer> rs = new HashMap();
		if (list == null || list.isEmpty()) {
			return rs;
		}
		for (Map<String, Object> map : list) {
			if (map.containsKey("status") && map.containsKey("count")) {
				rs.put(map.get("status").toString(), Integer.parseInt(map.get("count").toString()));
			}
		}
		return rs;
	}

	@Override
	public List<FinancingTargetDO> findByStatus(String status) {
		return queryDao.selectList("FINANCING_TARGET.findByStatus", status);
	}

	@Override
	public boolean checkBidRecordIsRight(String financingTargetId) {
		return queryDao.selectOne("FINANCING_TARGET.checkBidRecordIsRight", financingTargetId);
	}

	@Override
	public FinancingTargetDO findByCode(String targetCode) {
		return queryDao.selectOne("FINANCING_TARGET.findByCode", targetCode);
	}

	@Override
	public FinancingTargetDO getSaleAble(String financingTargetId) {
		return queryDao.selectOne("FINANCING_TARGET.getSaleAble", financingTargetId);
	}

	@Override
	public boolean subtractStock(String financingTargetId, BigDecimal amount) {
		Map map = new HashMap();
		map.put("amount", amount);
		map.put("id", financingTargetId);
		return updateDao.update("FINANCING_TARGET.subtractStock", map) == 1;
	}

	@Override
	public boolean create(FinancingTargetDO financingTargetDO) {
		financingTargetDO.setFinishAmount(BigDecimal.ZERO);
		if(financingTargetDO.isNewInvestor()){
			financingTargetDO.setId("NEW_"+StringUtil.genGUID());
		}
		return super.create(financingTargetDO);
	}

	@Override
	public List<FinancingTargetDO> findByPosition(PositionConditionVO condition) {
		return queryDao.selectList("FINANCING_TARGET.findByPosition", condition);
	}

	@Override
	public Date getFirstProfitDate(String profitFlag, Date payedTime) {
		if (FinancingTargetConstant.PROFIT_FLAG_TODAY.equalsIgnoreCase(profitFlag)) {
			return payedTime;
		} else if (FinancingTargetConstant.PROFIT_FLAG_TOMORROW.equalsIgnoreCase(profitFlag)) {
			Date date = new Date(payedTime.getTime());
			date.setDate(date.getDate() + 1);
			date.setHours(0);
			date.setMinutes(0);
			date.setSeconds(0);
			return date;
		}
		Date date = new Date(payedTime.getTime());
		if (payedTime.getDay() == 5) {
			// 周五
			date.setDate(date.getDate() + 3);
		} else if (payedTime.getDay() == 6) {
			// 周六
			date.setDate(date.getDate() + 2);
		} else {
			date.setDate(date.getDate() + 1);
		}
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;

	}

	@Override
	public boolean changeStatus(String fromStatus, String toStatus, String id) {
		Map map = new HashMap();
		map.put("fromStatus", fromStatus);
		map.put("toStatus", toStatus);
		map.put("id", id);
		return updateDao.update("FINANCING_TARGET.changeStatus", map) == 1;
	}

	@Override
	public FinancingTargetDO find(String tag, BigDecimal amount) {
		Map map = new HashMap();
		map.put("tag", tag);
		map.put("amount", amount);
		return queryDao.selectOne("FINANCING_TARGET.findOneMatchTagAndAmount", map);
	}

	public FinancingTargetServiceImpl() {

	}

}
